package com.mxf.agricultural.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mxf.agricultural.common.ErrorCode;
import com.mxf.agricultural.constant.CommonConstant;
import com.mxf.agricultural.exception.BusinessException;
import com.mxf.agricultural.exception.ThrowUtils;
import com.mxf.agricultural.mapper.CommodityMapper;
import com.mxf.agricultural.model.dto.commodity.CommodityQueryRequest;
import com.mxf.agricultural.model.entity.*;
import com.mxf.agricultural.model.vo.CommodityVO;
import com.mxf.agricultural.model.vo.UserVO;
import com.mxf.agricultural.service.CommodityService;
import com.mxf.agricultural.service.UserService;
import com.mxf.agricultural.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author JD
* @description 针对表【commodity(商品)】的数据库操作Service实现
* @createDate 2024-02-11 19:11:26
*/
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity>
    implements CommodityService {
    @Resource
    private UserService userService;

    @Override
    public void validCommodity(Commodity commodity, boolean add) {
        if (commodity == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String title = commodity.getName();
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(title), ErrorCode.PARAMS_ERROR);
        }

    }

    /**
     * 获取查询包装类
     *
     * @param commodityQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Commodity> getQueryWrapper(CommodityQueryRequest commodityQueryRequest) {
        String name = commodityQueryRequest.getName();
        Integer price = commodityQueryRequest.getPrice();
        String image = commodityQueryRequest.getImage();
        String description = commodityQueryRequest.getDescription();
        Integer statue = commodityQueryRequest.getStatue();
        Long userid = commodityQueryRequest.getUserid();
        int current = commodityQueryRequest.getCurrent();
        int pageSize = commodityQueryRequest.getPageSize();
        String sortField = commodityQueryRequest.getSortField();
        String sortOrder = commodityQueryRequest.getSortOrder();

        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        if (commodityQueryRequest == null) {
            return queryWrapper;
        }


        // 拼接查询条件
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userid), "userId", userid);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }



    @Override
    public Page<CommodityVO> getCommodityVOPage(Page<Commodity> commodityPage, HttpServletRequest request) {
        List<Commodity> commodityList = commodityPage.getRecords();
        Page<CommodityVO> commodityVOPage = new Page<>(commodityPage.getCurrent(), commodityPage.getSize(), commodityPage.getTotal());
        if (CollUtil.isEmpty(commodityList)) {
            return commodityVOPage;
        }
        // 1. 关联查询用户信息
        Set<Long> userIdSet = commodityList.stream().map(Commodity::getUserid).collect(Collectors.toSet());
        Map<Long, List<User>> userIdUserListMap = userService.listByIds(userIdSet).stream()
                .collect(Collectors.groupingBy(User::getId));

        return commodityVOPage;
    }

}




